/*
 * @Author: LiYuan
 * @Date: 2021-03-08 10:05:48
 * @LastEditTime: 2021-05-24 16:37:32
 * @LastEditors: LiYuan
 * @Description: 
 * You build it, You run it.
 */
const KoaRouter = require('koa-router')
const HttpClient = require('../util/HttpClient')
const Template = require('../util/Template')

const router = new KoaRouter();
const modules = require('../../modules');

// Page-服务事件
router.get('/event', async ctx => {
  let script = ['/event.list.js'];
  let modConfig = modules['event.list'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request('/event', 'get', null, {headers: reqHeaders});
  let body = result.body;
  ctx.body = await Template.renderWithHtml('事件列表', body, script, modConfig.template, ctx)
});

// Page-服务订单
router.get('/event/order', async ctx => {
  let script = ['/event.order.list.js'];
  let modConfig = modules['event.order.list'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request('/event/order', 'get', null, {headers: reqHeaders});
  let body = result.body;
  ctx.body = await Template.renderWithHtml('服务订单列表', body, script, modConfig.template, ctx)
});

// Page-事件创建客户
router.get('/event/createCustomer', async ctx => {
  let modConfig = modules['customer.edit'];
  let reqHeaders = ctx.request.headers;
  let script = ['/customer.edit.js'];
  let result = await HttpClient.request(`/event/createCustomer?eventId=${ctx.query.eventId}&goTo=${ctx.query.goTo}&flow=${ctx.query.flow}`, 'get', null, { headers: reqHeaders });
  let body = result.body;

  ctx.body = await Template.renderWithHtml('新建客户', body, script, modConfig.template, ctx)
});

// API-获取新建/编辑 initdata
router.get('/event/edit/info', (ctx) => {
  return HttpClient.proxy(ctx);
});

// API-获取事件详情 initdata
router.get('/event/view/info', (ctx) => {
  return HttpClient.proxy(ctx);
});

// API-获取事件日志列表
router.get('/event/view/eventRecord', (ctx) => {
  return HttpClient.proxy(ctx);
});

// 事件编辑/新建
router
  .get('/event/edit', async ctx => {
    await eventEditContent(ctx);
  })
  .get('/event/edit/:id', async ctx => {
    await eventEditContent(ctx);
  })
  .get('/event/edit4CallCenter', async ctx => {
    await eventEditContent(ctx);
  })
  .get('/event/copyEvent', async ctx => {
    await eventEditContent(ctx);
  })
  .get('/event/createFromCustomer/:id', async ctx => {
    await eventEditContent(ctx);
  })
  .get('/event/createFromProduct/:id', async ctx => {
    await eventEditContent(ctx);
  })

// Page-事件详情
router.get('/event/view/:id', async ctx => {
  let script = ['/event.detail.view.js'];
  let modConfig = modules['event.detail.view'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request(`/event/view/${ctx.params.id}`, 'get', null, {headers: reqHeaders});
  let body = result.body;
  ctx.body = await Template.renderWithHtml('', body, script, modConfig.template, ctx)
});

router
  .get('/event/convent2Task/jump', async ctx => {
    await taskEditAllotContent(ctx);
  })

async function eventEditContent(ctx) {
  let script = ['/event.detail.edit.js'];
  let modConfig = modules['event.detail.edit'];
  
  let { url, headers} = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;
  
  ctx.body = await Template.renderWithHtml('', body, script, modConfig.template, ctx)
}

async function taskEditAllotContent(ctx) {
  let modConfig = modules['task.edit.allot'];
  let script = ['/task.edit.allot.js'];
  
  let { url, headers} = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;
  
  ctx.body = await Template.renderWithHtml('', body, script, modConfig.template, ctx)
}

module.exports = router;